//
//  CheckViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/8/21.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "CheckViewController.h"

static NSString *WalletRecordCellWithIdentifier     = @"WalletRecordCell";
static NSString *FundStatisticsCellWithIdentifier   = @"FundStatisticsCell";

@interface CheckViewController ()

@end

@implementation CheckViewController

#pragma mark - Set
- (void)setQueryDate:(NSDate *)queryDate
{
    _queryDate = queryDate;
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    NSString *locationString=[dateformatter stringFromDate:_queryDate];
    [self.changeDateButton setTitle:locationString forState:UIControlStateNormal];
}


#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
}

- (void)createUI
{
    self.queryDate = [NSDate date];
    [self.changeDateButton.layer setCornerRadius:kNomalRadius];
    self.screenDateView.contents = [[NSMutableArray alloc] initWithObjects:@"日", @"月", @"年", nil];
    self.screenDateView.delegate = self;
    self.queryDateItem = @"日";
    self.selectDateView = [[SelectDateView alloc] initWithDelegate:self];
    [self.selectDateView.datePicker setDatePickerMode:UIDatePickerModeDate];
    self.recordView.delegate = self;
    self.recordView.dataSource = self;
    [self upData];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.walletRecord.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0f;
}

/*! @brief Cell单元格 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.walletRecord[indexPath.row];
    NSDate *recordDtate = dict[@"RecordDtate"];
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    if ([self.queryDateItem isEqualToString:@"日"]) {
        WalletRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:WalletRecordCellWithIdentifier];
        if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:WalletRecordCellWithIdentifier owner:nil options:nil];
            cell = [nibs lastObject];
        }
        [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
        [cell.moneyFrom setText:dict[@"MoneyFrom"]];
        [cell.moneyFor setText:dict[@"MoneyFor"]];
        [cell.remarks setText:dict[@"Remarks"]];
        [cell.recordDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyText:dict[@"Money"] Save:[dict[@"CheckoutType"] isEqualToString:@"1"]];
        return cell;
    } else if ([self.queryDateItem isEqualToString:@"月"]) {
        FundStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:FundStatisticsCellWithIdentifier];
        if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:FundStatisticsCellWithIdentifier owner:nil options:nil];
            cell = [nibs lastObject];
        }
        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        [cell.statisticsDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyTextWithPay:dict[@"payMoney"] Income:dict[@"incomeMoney"]];
        return cell;
    } else if ([self.queryDateItem isEqualToString:@"年"]) {
        FundStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:FundStatisticsCellWithIdentifier];
        if (cell == nil) {
            NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:FundStatisticsCellWithIdentifier owner:nil options:nil];
            cell = [nibs lastObject];
        }
        [dateformatter setDateFormat:@"YYYY-MM"];
        [cell.statisticsDate setText:[dateformatter stringFromDate:recordDtate]];
        [cell moneyTextWithPay:dict[@"payMoney"] Income:dict[@"incomeMoney"]];
        return cell;
    }
    return nil;
}

#pragma mark - TableView配置 - 操作
/*! @brief 点击了单元格 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = self.walletRecord[indexPath.row];
    if (data && data[@"RecordDtate"]) {
        self.queryDate = data[@"RecordDtate"];
    }
    if ([self.queryDateItem isEqualToString:@"月"]) {
        [self.screenDateView clickButtonAtIndex:0];
        //self.queryDateItem = @"日";
    } else if ([self.queryDateItem isEqualToString:@"年"]) {
        [self.screenDateView clickButtonAtIndex:1];
    }
    [self upData];
}


#pragma mark - 修改时间
- (IBAction)changeDate:(UIButton *)sender {
    [ActionView showView:self.selectDateView direction:PopViewDirectionStyleCenter];
}

- (IBAction)changeDateToNew:(UIButton *)sender {
    self.queryDate = [NSDate date];
    [self upData];
}

#pragma mark - 时间选择代理
- (void)selectDate:(SelectDateView *)picker
{
    self.queryDate = picker.datePicker.date;
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
    [self upData];
}
- (void)cancelSelect:(SelectDateView *)picker
{
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}

#pragma mark - XFYSelectScrollView代理
- (void)operation:(XFYRadioView *)selectView Button:(UIButton *)button AtIndex:(NSInteger)index
{
    self.queryDateItem = button.titleLabel.text;
    [self upData];
}

- (void)upData
{
    NSMutableDictionary *incomeAndPayDict;
    DataDateType dateType = DataDateTypeUnlimited;
    if ([self.queryDateItem isEqualToString:@"日"]) {
        dateType = DataDateTypeDay;
        self.walletRecord = [[WalletLocalData sharedManager] recordArrayWithDate:self.queryDate DataDateType:dateType];
    } else if ([self.queryDateItem isEqualToString:@"月"]) {
        dateType = DataDateTypeMonth;
        self.walletRecord = [[WalletLocalData sharedManager] statisticsArrayWithDate:self.queryDate DataDateType:dateType];
    } else if ([self.queryDateItem isEqualToString:@"年"]) {
        dateType = DataDateTypeYear;
        self.walletRecord = [[WalletLocalData sharedManager] statisticsArrayWithDate:self.queryDate DataDateType:dateType];
    }
    self.payCartogram.contents = [[WalletLocalData sharedManager] rtatisticsPayWithDate:self.queryDate DataDateType:dateType];
    self.incomeCartogram.contents = [[WalletLocalData sharedManager] rtatisticsIncomeWithDate:self.queryDate DataDateType:dateType];
    incomeAndPayDict = [[WalletLocalData sharedManager] incomeAndPayWithDate:self.queryDate DataDateType:dateType];
    [self.recordView reloadData];
    NSNumber *pay = incomeAndPayDict[@"pay"];
    NSNumber *income = incomeAndPayDict[@"income"];
    NSNumber *sum = [[NSNumber alloc] initWithFloat:[income floatValue] - [pay floatValue]];
    NSString *format = [NSString stringWithFormat:@"%%.%df",2];
    NSString *payStr = [NSString stringWithFormat:format,[pay floatValue]];
    NSString *incomeStr = [NSString stringWithFormat:format,[income floatValue]];
    NSString *sumStr = [NSString stringWithFormat:format,[sum floatValue]];
    [self.payLabel setText:[NSString stringWithFormat:@"支出:%@",payStr]];
    [self.incomeLabel setText:[NSString stringWithFormat:@"收入:%@",incomeStr]];
    [self.sumLabel setText:[NSString stringWithFormat:@"汇总:%@",sumStr]];
    [self.contrastView setContrastWithLeft:[pay floatValue] WithRight:[income floatValue]];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
